package com.example.Repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.Repository.CollectRepository;
import com.example.pojo.Result;
import com.example.pojo.TrendsCollect;
import com.example.pojo.dto.LikeDto;
import com.example.pojo.vo.TrendsVo;
import com.example.service.TrendsCollectService;
import com.example.utils.ConditionVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * @author crt
 */
@Service
public class CollectRepositoryImpl implements CollectRepository {

    @Resource
    TrendsCollectService trendsCollectService;
    @Resource
    TrendsRepositoryImpl trendsRepository;

    @Override
    public Result<String> trendsCollect(LikeDto likeDto) {

        QueryWrapper<TrendsCollect> queryWrapper = new QueryWrapper<TrendsCollect>().eq("user_id",likeDto.getUserId())
                .eq("trends_id",likeDto.getLikeId());
        TrendsCollect trendsCollect = trendsCollectService.getOne(queryWrapper);
        if (trendsCollect == null){
            TrendsCollect trendsCollect1 = new TrendsCollect(likeDto.getUserId(),likeDto.getLikeId());
            trendsCollectService.save(trendsCollect1);
        }else {
            trendsCollectService.removeById(trendsCollect);
        }

        return Result.success("操作成功");
    }

    @Override
    public Result<List<TrendsVo>> collectList(Integer userId) {
        List<TrendsCollect> trendsCollectList = trendsCollectService.list(new QueryWrapper<TrendsCollect>().eq("user_id", userId));
        List<TrendsVo> trendsVoList = new ArrayList<>();
        for (TrendsCollect trendsCollect : trendsCollectList){
            ConditionVo conditionVo = new ConditionVo("trends_id",trendsCollect.getTrendsId().toString(),"eq");
            List<ConditionVo> conditionVoList = new ArrayList<>();
            conditionVoList.add(conditionVo);
            List<TrendsVo> trendsList = trendsRepository.getTrendsVoList(conditionVoList);
            trendsVoList.addAll(trendsList);
        }

        return Result.success(trendsVoList);
    }


}
